<?php

class FForm
{
    
    protected $name;
    protected $title;
    protected $info;
    protected $padding = '30 4 4 10';
    
    protected $width = '800';
    protected $height = '600';
    
    protected $showBottomBar = true;
        
    protected $formFields;
    protected $formButtons;
    protected $formFunction;
    
    protected $controller = 'FDefaultController';
    protected $model = 'FDefaultModel';
    protected $tableName = '';
    
    protected $closable = true;
    protected $draggable = true;
    
    public function __construct($name = 'fform')
    {
        if ( $name == 'fform' )
        {
            $name .= '_' . rand();
        }
        
        $this->name = $name;
    }

    public function generate($show = false)
    {
        if ( !$this->controller instanceof FController )
        {
            $controller = $this->instantiateController();                                    
        }
        else
        {
            $controller = $this->controller;
        }
        
        $controller instanceof FController;
        
        // Tabela
        if ( !$this->tableName )
        {
            $this->tableName = $controller->getTable();
        }
        
        // Campos
        foreach( $this->formFields as $field )
        {
            $fields[] = $field->generate();
        }
        $fields = implode(',', $fields);
        
        // Botões
        foreach( $this->formButtons as $button )
        {
            $buttons[] = $button->generate();
        }
        $buttons = implode(',', $buttons);
        
        // Bottom Bar
        $bbar = $this->showBottomBar ? 'true' : 'false';
        
        //Closable & Draggable
        $closable = $this->closable ? 'true' : 'false';
        $draggable = $this->draggable ? 'true' : 'false';
        
        $jsCode =
        "
                Ext.define('{$this->name}', {
                    extend: 'FForm',
                    title: '{$this->title}',
                    info: '{$this->info}',
                    showBBar: $bbar,
                    width: {$this->width},
                    height: {$this->height},
                    closable: {$closable},
                    draggable: {$draggable},
                    function: '{$this->formFunction}',
                    initComponent: function() {
                        
                        this.formPanel = new Ext.form.FormPanel(
                            {
                                cls: 'form',
                                baseCls: 'x-fieldset-noborder',
                                flex: 1,
                                width: '100%',
                                padding: '{$this->getPadding()}',
                                items: [$fields]
                            });
                            
                        this.formFields = this.formPanel;

                        this.buttons = [
                            $buttons
                        ];
                        
                        this.callParent();
                    },
                    _submit: function(item, e) {
                        var form = this.formPanel.getForm();
                        
                        if ( !form.isValid() )
                        {
                            Ext.create('FMessageAlert', {
                                msg: '" . ERRO_CAMPOS_NECESSARIOS . "'
                            }).show();
                        }
                        else
                        {
                            form.submit({
                                url: this.handlerUrl,
                                params: {
                                    manager: '" . $this->model . "',                                        
                                    action: 'submit',
                                    function: item.function
                                },
                                scope: this,
                                success: function(form, action)
                                {
                                    this.obj = Ext.JSON.decode(action.response.responseText);
                                    
                                    if ( this.obj.closeForm )
                                    {
                                        if ( this.obj.showSuccessMsg )
                                        {
                                            Ext.create('FMessageSuccess', {
                                                msg: this.obj.msg
                                            }).show();
                                        }
                                        
                                        this.doClose();
                                    }
                                    else
                                    {
                                        if ( this.obj.showSuccessMsg )
                                        {
                                            Ext.create('FMessageSuccess', {
                                                msg: this.obj.msg
                                            }).show();
                                        }
                                    }
                                    
                                    this._reset();
                                    
                                    if ( this.obj.action )
                                    {
                                        eval(this.obj.action);
                                    }
                                },
                                failure: function(form, action)
                                {
                                    this.obj = Ext.JSON.decode(action.response.responseText);
                                    Ext.create('FMessageError', {
                                        msg: this.obj.msg
                                    }).show();
                                    
                                    if ( this.obj.closeForm )
                                    {
                                        if ( this.obj.confirmOnClose )
                                        {
                                            Ext.Msg.confirm('Confirmação', this.obj.onCloseConfirmMsg, function(btn)
                                            {
                                                if( btn == 'yes' )
                                                {
                                                    this.doClose();
                                                }
                                            }, this);
                                        }
                                        else
                                        {
                                            this.doClose();
                                        }
                                    }
                                }
                            });
                        }
                    },
                    _close: function(item, e) {
                        this._reset();
                        this.doClose();
                    },
                    _reset: function() {
                        var form = this.formPanel.getForm();
                        form.reset();
                    }
                });
        ";
        
        echo($jsCode);
                    
        if ( $show )
        {
            $this->show();
        }
    }
    
    public function show()
    {
        echo " var fform_{$this->name} = Ext.create('{$this->name}'); ";
        
        $jsCode = "            
            fform_{$this->name}.show();
        ";
            
        return $jsCode;
    }
    
    protected function instantiateController()
    {
        if ( class_exists($this->controller) )
        {
            $controller = new $this->controller();
        }
        else
        {
            $controller = new FDefaultController();
        }
        
        return $controller;
    }
    
    public function getName() {
        return $this->name;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function getTitle() {
        return $this->title;
    }

    public function setTitle($title) {
        $this->title = $title;
    }

    public function getInfo() {
        return $this->info;
    }

    public function setInfo($info) {
        $this->info = $info;
    }

    public function getFormFields() {
        return $this->formFields;
    }

    public function setFormFields($formFields) {
        if ( is_array($formFields) )
        {
            $this->formFields = $formFields;
        }
    }
    
    public function addFormField($field) {
        $this->formFields[] = $field;
    }
    
    public function getFormButtons() {
        return $this->formButtons;
    }

    public function setFormButtons($formButtons) {
        $this->formButtons = $formButtons;
    }
    
    public function addFormButton($formButton)
    {
        $this->formButtons[] = $formButton;
    }
   
    public function getController() {
        return $this->controller;
    }

    public function setController($controller) {
        $this->controller = $controller;
    }
    
    public function getShowBottomBar() {
        return $this->showBottomBar;
    }

    public function setShowBottomBar($showBottomBar) {
        $this->showBottomBar = $showBottomBar;
    }
    public function getWidth() {
        return $this->width;
    }

    public function setWidth($width) {
        $this->width = $width;
    }

    public function getHeight() {
        return $this->height;
    }

    public function setHeight($height) {
        $this->height = $height;
    }
    
    public function getTableName() {
        return $this->tableName;
    }

    public function setTableName($tableName) {
        $this->tableName = $tableName;
    }
    
    public function getFormFunction() {
        return $this->formFunction;
    }

    public function setFormFunction($formFunction) {
        $this->formFunction = $formFunction;
    }

    public function getPadding() {
        return $this->padding;
    }

    public function setPadding($padding) {
        $this->padding = $padding;
    }
    
    public function getClosable() {
        return $this->closable;
    }

    public function setClosable($closable) {
        $this->closable = $closable;
    }

    public function getDraggable() {
        return $this->draggable;
    }

    public function setDraggable($draggable) {
        $this->draggable = $draggable;
    }
    
    public function getModel() {
        return $this->model;
    }

    public function setModel($model) {
        $this->model = $model;
    }

}

?>
